const carousel = document.querySelector('.carousel');
const carouselItems = document.querySelectorAll('.carousel-item');
const nextButton = document.querySelector('.carousel-button-next');
const prevButton = document.querySelector('.carousel-button-prev');
 
let activeItem = 0;
 
const showItem = (index) => {
  carouselItems.forEach((item, i) => {
    item.classList.toggle('active', i === index);
  });
  activeItem = index;
};
 
const nextItem = () => {
  const nextIndex = (activeItem + 1) % carouselItems.length;
  showItem(nextIndex);
};
 
const prevItem = () => {
  const prevIndex = (activeItem - 1 + carouselItems.length) % carouselItems.length;
  showItem(prevIndex);
};
 
const autoPlay = () => {
  setInterval(() => {
    nextItem();
  }, 2000); // 每2秒自动播放下一张
};
 
// 初始显示第一张
showItem(0);
// 启动自动播放
autoPlay();
 
nextButton.addEventListener('click', nextItem);
prevButton.addEventListener('click', prevItem);